package com.site.web.sys.controller;

import com.site.domain.CoreBusiness;
import com.site.domain.UserInfo;
import com.site.service.CoreBusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description:
 * @auther: junliang
 * @date: 2018/9/1 11:25
 */
@Controller
@RequestMapping({"/core/"})
public class CoreController {
    private static Logger logger = LoggerFactory.getLogger(CoreController.class);
    private static String SYS_LOGIN = "redirect:/sys/login";
    private static String FIND_CORE = "redirect:/core/findCore";
    @Autowired
    private CoreBusinessService coreBusinessService;

    @RequestMapping( value = {"findCore"},method = {RequestMethod.GET})
    public ModelAndView findCore(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        UserInfo userInfo = null;
        userInfo = (UserInfo)WebUtils.getSessionAttribute(request, "userInfo");
        if (null == userInfo) {
            WebUtils.setSessionAttribute(request, "userInfo", (Object)null);
            return new ModelAndView(SYS_LOGIN);
        } else {
            List coreBusinessList = null;

            try {
                coreBusinessList = this.coreBusinessService.findCoreBusiness();
                if (null != coreBusinessList) {
                    mav.addObject("coreBusinessList", coreBusinessList);
                }
            } catch (Exception var7) {
                var7.printStackTrace();
                logger.error("查询异常:" + var7.getMessage() + "");
            }

            return mav;
        }
    }

    @RequestMapping( value = {"addCore"},method = {RequestMethod.GET})
    public ModelAndView addCore(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView();
        UserInfo userInfo = null;
        userInfo = (UserInfo)WebUtils.getSessionAttribute(request, "userInfo");
        if (null == userInfo) {
            WebUtils.setSessionAttribute(request, "userInfo", (Object)null);
            return new ModelAndView(SYS_LOGIN);
        } else {
            return mav;
        }
    }

    @RequestMapping(value = {"addCore"},method = {RequestMethod.POST})
    public ModelAndView addTypical(HttpServletRequest request, HttpServletResponse response, @ModelAttribute CoreBusiness coreBusiness) {
        ModelAndView mav = new ModelAndView();
        UserInfo userInfo = null;
        userInfo = (UserInfo)WebUtils.getSessionAttribute(request, "userInfo");
        if (null == userInfo) {
            WebUtils.setSessionAttribute(request, "userInfo", (Object)null);
            return new ModelAndView(SYS_LOGIN);
        } else {
            int resultCode = 0;
            String resultMsg = "";

            try {
                this.coreBusinessService.addCoreBusiness(coreBusiness);
                resultCode = 1;
                resultMsg = "添加成功";
            } catch (Exception var9) {
                var9.printStackTrace();
                logger.error("添加异常:" + var9.getMessage() + "");
                resultMsg = "添加异常";
            }

            mav.addObject("resultCode", Integer.valueOf(resultCode));
            mav.addObject("resultMsg", resultMsg);
            return mav;
        }
    }

    @RequestMapping(value = {"updCore"},method = {RequestMethod.GET})
    public ModelAndView updCore(HttpServletRequest request, HttpServletResponse response, String id) {
        ModelAndView mav = new ModelAndView();
        UserInfo userInfo = null;
        userInfo = (UserInfo)WebUtils.getSessionAttribute(request, "userInfo");
        if (null == userInfo) {
            WebUtils.setSessionAttribute(request, "userInfo", (Object)null);
            return new ModelAndView(SYS_LOGIN);
        } else {
            int resultCode = 0;
            String resultMsg = "";
            CoreBusiness coreBusiness = null;

            try {
                coreBusiness = this.coreBusinessService.getCoreBusinessById(Integer.parseInt(id));
                if (null != coreBusiness) {
                    resultCode = 1;
                    mav.addObject("coreBusiness", coreBusiness);
                }
            } catch (NumberFormatException var10) {
                var10.printStackTrace();
                logger.error("参数ID类型转换异常:" + var10.getMessage() + "");
                resultMsg = "获取失败，无编号ID";
            } catch (Exception var11) {
                var11.printStackTrace();
                logger.error("查询异常:" + var11.getMessage() + "");
                resultMsg = "获取失败，查询异常";
            }

            mav.addObject("resultCode", Integer.valueOf(resultCode));
            mav.addObject("resultMsg", resultMsg);
            return mav;
        }
    }

    @RequestMapping( value = {"updCore"},method = {RequestMethod.POST})
    public ModelAndView updCore(HttpServletRequest request, HttpServletResponse response, @ModelAttribute CoreBusiness coreBusiness) {
        ModelAndView mav = new ModelAndView();
        UserInfo userInfo = null;
        userInfo = (UserInfo)WebUtils.getSessionAttribute(request, "userInfo");
        if (null == userInfo) {
            WebUtils.setSessionAttribute(request, "userInfo", (Object)null);
            return new ModelAndView(SYS_LOGIN);
        } else {
            int resultCode = 0;
            String resultMsg = "";

            try {
                this.coreBusinessService.updCoreBusiness(coreBusiness);
                resultCode = 1;
                resultMsg = "修改成功";
            } catch (Exception var9) {
                var9.printStackTrace();
                logger.error("修改异常:" + var9.getMessage() + "");
                resultMsg = "修改异常";
            }

            mav.addObject("resultCode", Integer.valueOf(resultCode));
            mav.addObject("resultMsg", resultMsg);
            return mav;
        }
    }

    @RequestMapping( value = {"delCoreById"},method = {RequestMethod.GET})
    public ModelAndView delCoreById(HttpServletRequest request, HttpServletResponse response, String id) {
        ModelAndView mav = new ModelAndView(FIND_CORE);
        UserInfo userInfo = null;
        userInfo = (UserInfo)WebUtils.getSessionAttribute(request, "userInfo");
        if (null == userInfo) {
            WebUtils.setSessionAttribute(request, "userInfo", (Object)null);
            return new ModelAndView(SYS_LOGIN);
        } else {
            byte resultCode = 0;

            try {
                this.coreBusinessService.delCoreBusinessById(Integer.parseInt(id));
                resultCode = 1;
            } catch (Exception var8) {
                var8.printStackTrace();
                logger.error("删除异常:" + var8.getMessage() + "");
            }

            mav.addObject("resultCode", Integer.valueOf(resultCode));
            return mav;
        }
    }
}